<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"llmysnow.top","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="安装 i18ninternationalization 这个单词中，i 和 n 之间有 18 个字母">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue+Element 国际化(i18n)页面全覆盖（路由、表单检验、弹出框等）">
<meta property="og:url" content="http://llmysnow.top/posts/82482c19/index.html">
<meta property="og:site_name" content="奇幻的梦之旅">
<meta property="og:description" content="安装 i18ninternationalization 这个单词中，i 和 n 之间有 18 个字母">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/js-img/%E5%9B%BD%E9%99%85%E5%8C%96%E8%AF%AD%E8%A8%80%E6%96%87%E4%BB%B6.jpg">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/company-img/%E5%88%87%E6%8D%A2%E8%AF%AD%E8%A8%80.gif">
<meta property="article:published_time" content="2021-05-06T08:38:25.955Z">
<meta property="article:modified_time" content="2022-04-23T11:27:04.398Z">
<meta property="article:author" content="LiLyn">
<meta property="article:tag" content="Vue">
<meta property="article:tag" content="Element">
<meta property="article:tag" content="i18n">
<meta property="article:tag" content="切换语言">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/lilyn/pic/raw/master/js-img/%E5%9B%BD%E9%99%85%E5%8C%96%E8%AF%AD%E8%A8%80%E6%96%87%E4%BB%B6.jpg">

<link rel="canonical" href="http://llmysnow.top/posts/82482c19/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Vue+Element 国际化(i18n)页面全覆盖（路由、表单检验、弹出框等） | 奇幻的梦之旅</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="奇幻的梦之旅" type="application/atom+xml">
</head>
<!--即时预加载技术，在用户点击之前预先加载页面-->
<script src="https://cdn.jsdelivr.net/gh/TRHX/CDN-for-itrhx.com@2.0.2/js/instantclick-1.2.2.js" type="module"></script>
<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>
	  <!--<a target="_blank" rel="noopener" href="https://github.com/llwodexue" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>-->
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">奇幻的梦之旅</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">http://llmysnow.top/</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://llmysnow.top/posts/82482c19/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/logo.jpg">
      <meta itemprop="name" content="LiLyn">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="奇幻的梦之旅">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Vue+Element 国际化(i18n)页面全覆盖（路由、表单检验、弹出框等）
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-05-06 16:38:25" itemprop="dateCreated datePublished" datetime="2021-05-06T16:38:25+08:00">2021-05-06</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Vue/" itemprop="url" rel="index"><span itemprop="name">Vue</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>22k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>20 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="安装-i18n"><a href="#安装-i18n" class="headerlink" title="安装 i18n"></a>安装 i18n</h1><p><code>internationalization</code> 这个单词中，i 和 n 之间有 18 个字母</p>
<a id="more"></a>

<p><strong>注意：</strong> 当前 i18n 最新版本为 9 版本，9 版本没有 VueI18n，<code>import VueI18n from &#39;vue-i18n&#39;</code> 解构会报错 <code>Cannot read property &#39;install&#39; of undefined</code></p>
<ul>
<li>这里为了让 Element 兼容，安装的是 8 版本的</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install vue-i18n@8</span><br></pre></td></tr></table></figure>

<h1 id="基础配置篇"><a href="#基础配置篇" class="headerlink" title="基础配置篇"></a>基础配置篇</h1><h2 id="1-页面中使用国际化"><a href="#1-页面中使用国际化" class="headerlink" title="1.页面中使用国际化"></a>1.页面中使用国际化</h2><h3 id="i18n-文件夹下的-index-js-文件"><a href="#i18n-文件夹下的-index-js-文件" class="headerlink" title="* i18n 文件夹下的 index.js 文件"></a>* i18n 文件夹下的 index.js 文件</h3><blockquote>
<p><code>require.context</code></p>
<p>它允许传入一个目录进行搜索，一个标志指示是否应该搜索子目录，还有一个正则表达式来匹配文件</p>
<ul>
<li>第一个参数：<code>directory</code> 要搜索的文件夹目录不能是变量，否则在编译阶段无法定位目录</li>
<li>第二个参数：<code>useSubdirectories</code> 是否搜索子目录</li>
<li>第三个参数：<code>regExp</code> 匹配文件的正则表达式</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">require</span>.context(<span class="string">&#x27;demo&#x27;</span>, <span class="literal">false</span>, <span class="regexp">/\.js$/</span>)</span><br></pre></td></tr></table></figure>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* i18n/index.js */</span></span><br><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> VueI18n <span class="keyword">from</span> <span class="string">&#x27;vue-i18n&#x27;</span></span><br><span class="line"></span><br><span class="line">Vue.use(VueI18n)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> langFiles = <span class="built_in">require</span>.context(<span class="string">&#x27;./config&#x27;</span>, <span class="literal">false</span>, <span class="regexp">/\.js$/</span>)</span><br><span class="line"><span class="keyword">const</span> regExp = <span class="regexp">/\.\/([^\.\/]+)\.([^\.]+)$/</span></span><br><span class="line"><span class="keyword">const</span> messages = &#123;&#125;</span><br><span class="line"><span class="keyword">const</span> lang = &#123;</span><br><span class="line">  zh: zhCNLocale,</span><br><span class="line">  zhTW: zhTWLocale,</span><br><span class="line">  en: enLocale</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 默认语言</span></span><br><span class="line"><span class="keyword">const</span> loadLanguage = <span class="string">&#x27;zh&#x27;</span></span><br><span class="line"></span><br><span class="line">langFiles.keys().forEach(<span class="function"><span class="params">key</span> =&gt;</span> &#123;</span><br><span class="line">  messages[k] = langFiles(key).default</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getLanguage</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 第一次进入页面或手动清除设置默认语言</span></span><br><span class="line">  <span class="built_in">localStorage</span>.getItem(<span class="string">&#x27;lang&#x27;</span>) ? <span class="literal">null</span> : <span class="built_in">localStorage</span>.setItem(<span class="string">&#x27;lang&#x27;</span>, loadLanguage)</span><br><span class="line">  <span class="keyword">let</span> locale = <span class="built_in">localStorage</span>.getItem(<span class="string">&#x27;lang&#x27;</span>)</span><br><span class="line">  <span class="keyword">if</span> (!(locale <span class="keyword">in</span> messages)) locale = loadLanguage</span><br><span class="line">  <span class="keyword">return</span> locale</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> i18n = <span class="keyword">new</span> VueI18n(&#123;</span><br><span class="line">  locale: getLanguage(),</span><br><span class="line">  messages</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> i18n</span><br></pre></td></tr></table></figure>

<ul>
<li>messages 对象</li>
</ul>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/js-img/%E5%9B%BD%E9%99%85%E5%8C%96%E8%AF%AD%E8%A8%80%E6%96%87%E4%BB%B6.jpg"></p>
<h3 id="main-js-引入"><a href="#main-js-引入" class="headerlink" title="main.js 引入"></a>main.js 引入</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* main.js */</span></span><br><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> App <span class="keyword">from</span> <span class="string">&#x27;./App.vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> i18n <span class="keyword">from</span> <span class="string">&#x27;./i18n&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Vue(&#123;</span><br><span class="line">  render: <span class="function"><span class="params">h</span> =&gt;</span> h(App),</span><br><span class="line">  i18n</span><br><span class="line">&#125;).$mount(<span class="string">&#x27;#app&#x27;</span>)</span><br></pre></td></tr></table></figure>

<h3 id="展示页面"><a href="#展示页面" class="headerlink" title="展示页面"></a>展示页面</h3><p>效果图如下：</p>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/company-img/%E5%88%87%E6%8D%A2%E8%AF%AD%E8%A8%80.gif"></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    &lt;el-dropdown</span><br><span class="line">      trigger=&quot;click&quot;</span><br><span class="line">      class=&quot;international&quot;</span><br><span class="line">      @command=&quot;handleSetLanguage&quot;</span><br><span class="line">    &gt;</span><br><span class="line">      <span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;<span class="name">i</span> <span class="attr">class</span>=<span class="string">&quot;el-icon-s-tools&quot;</span> /&gt;</span>切换语言<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">el-dropdown-menu</span> <span class="attr">slot</span>=<span class="string">&quot;dropdown&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">el-dropdown-item</span> <span class="attr">:disabled</span>=<span class="string">&quot;language === &#x27;zh&#x27;&quot;</span> <span class="attr">command</span>=<span class="string">&quot;zh&quot;</span>&gt;</span></span><br><span class="line">          中文</span><br><span class="line">        <span class="tag">&lt;/<span class="name">el-dropdown-item</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">el-dropdown-item</span> <span class="attr">:disabled</span>=<span class="string">&quot;language === &#x27;en&#x27;&quot;</span> <span class="attr">command</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">          English</span><br><span class="line">        <span class="tag">&lt;/<span class="name">el-dropdown-item</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">el-dropdown-menu</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">el-dropdown</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">el-table</span> <span class="attr">:data</span>=<span class="string">&quot;tableData&quot;</span> <span class="attr">style</span>=<span class="string">&quot;width: 100%&quot;</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">el-table-column</span> <span class="attr">prop</span>=<span class="string">&quot;date&quot;</span> <span class="attr">:label</span>=<span class="string">&quot;$t(&#x27;table.date&#x27;)&quot;</span> <span class="attr">width</span>=<span class="string">&quot;180&quot;</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">el-table-column</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">el-table-column</span> <span class="attr">prop</span>=<span class="string">&quot;name&quot;</span> <span class="attr">:label</span>=<span class="string">&quot;$t(&#x27;table.name&#x27;)&quot;</span> <span class="attr">width</span>=<span class="string">&quot;180&quot;</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">el-table-column</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">el-table-column</span> <span class="attr">prop</span>=<span class="string">&quot;age&quot;</span> <span class="attr">:label</span>=<span class="string">&quot;$t(&#x27;table.age&#x27;)&quot;</span>&gt;</span> <span class="tag">&lt;/<span class="name">el-table-column</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">el-table</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line"><span class="javascript">  <span class="function"><span class="title">data</span>(<span class="params"></span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">return</span> &#123;</span></span><br><span class="line">      tableData: [</span><br><span class="line">        &#123;</span><br><span class="line"><span class="javascript">          date: <span class="string">&#x27;2016-05-02&#x27;</span>,</span></span><br><span class="line"><span class="javascript">          name: <span class="string">&#x27;王小虎&#x27;</span>,</span></span><br><span class="line"><span class="javascript">          age: <span class="string">&#x27;23&#x27;</span></span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line"><span class="javascript">          date: <span class="string">&#x27;2016-05-04&#x27;</span>,</span></span><br><span class="line"><span class="javascript">          name: <span class="string">&#x27;李小鸭&#x27;</span>,</span></span><br><span class="line"><span class="javascript">          age: <span class="string">&#x27;14&#x27;</span></span></span><br><span class="line">        &#125;</span><br><span class="line">      ],</span><br><span class="line"><span class="javascript">      language: <span class="built_in">localStorage</span>.getItem(<span class="string">&#x27;lang&#x27;</span>)</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line"><span class="javascript">    <span class="function"><span class="title">handleSetLanguage</span>(<span class="params">lang</span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">      <span class="built_in">this</span>.$i18n.locale = lang</span></span><br><span class="line"><span class="javascript">      <span class="built_in">this</span>.language = lang</span></span><br><span class="line"><span class="javascript">      <span class="built_in">localStorage</span>.setItem(<span class="string">&#x27;lang&#x27;</span>, lang)</span></span><br><span class="line"><span class="javascript">      <span class="comment">// window.location.reload()</span></span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="语言配置文件"><a href="#语言配置文件" class="headerlink" title="语言配置文件"></a>语言配置文件</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* en.js */</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  table: &#123;</span><br><span class="line">    date: <span class="string">&#x27;Date&#x27;</span>,</span><br><span class="line">    name: <span class="string">&#x27;Name&#x27;</span>,</span><br><span class="line">    age: <span class="string">&#x27;Age&#x27;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* zh.js */</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  table: &#123;</span><br><span class="line">    date: <span class="string">&#x27;日期&#x27;</span>,</span><br><span class="line">    name: <span class="string">&#x27;姓名&#x27;</span>,</span><br><span class="line">    age: <span class="string">&#x27;年龄&#x27;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="英文首字母大写"><a href="#英文首字母大写" class="headerlink" title="英文首字母大写"></a>英文首字母大写</h3><p>英文情况下，需要对其进行大小写转换处理（让每个单词首字母大写）</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (<span class="keyword">const</span> k <span class="keyword">in</span> langs) &#123;</span><br><span class="line">  <span class="keyword">const</span> item = langs[k]</span><br><span class="line">  <span class="keyword">for</span> (<span class="keyword">const</span> j <span class="keyword">in</span> item) &#123;</span><br><span class="line">    item[j] = item[j].toLowerCase().replace(<span class="regexp">/( |^)[a-z]/g</span>, <span class="function"><span class="params">L</span> =&gt;</span> L.toUpperCase())</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="2-Element-国际化"><a href="#2-Element-国际化" class="headerlink" title="2.Element 国际化"></a>2.Element 国际化</h2><h3 id="main-js-引入-1"><a href="#main-js-引入-1" class="headerlink" title="main.js 引入"></a>main.js 引入</h3><ul>
<li>参考 <a target="_blank" rel="noopener" href="https://element.eleme.cn/#/zh-CN/component/i18n#jian-rong-vue-i18n-6-x">兼容 <code>vue-i18n@6.x</code></a>  的步骤（需要手动处理）</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* main.js */</span></span><br><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> App <span class="keyword">from</span> <span class="string">&#x27;./App.vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> i18n <span class="keyword">from</span> <span class="string">&#x27;./i18n&#x27;</span></span><br><span class="line"><span class="keyword">import</span> ElementUI <span class="keyword">from</span> <span class="string">&#x27;element-ui&#x27;</span></span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;element-ui/lib/theme-chalk/index.css&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 8版本以上需要这么使用</span></span><br><span class="line">Vue.use(ElementUI, &#123;</span><br><span class="line">  i18n: <span class="function">(<span class="params">key, value</span>) =&gt;</span> i18n.t(key, value),</span><br><span class="line">  size: <span class="string">&#x27;mini&#x27;</span></span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Vue(&#123;</span><br><span class="line">  render: <span class="function"><span class="params">h</span> =&gt;</span> h(App),</span><br><span class="line">  i18n</span><br><span class="line">&#125;).$mount(<span class="string">&#x27;#app&#x27;</span>)</span><br></pre></td></tr></table></figure>

<h3 id="i18n-文件夹下的-index-js-文件-1"><a href="#i18n-文件夹下的-index-js-文件-1" class="headerlink" title="* i18n 文件夹下的 index.js 文件"></a>* i18n 文件夹下的 index.js 文件</h3><p>在 index.js 中使用 <code>Object.assign</code>  合并有一个好处：方便扩展（相对于在语言配置表引入使用扩展运算符）</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* i18n/index.js */</span></span><br><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> VueI18n <span class="keyword">from</span> <span class="string">&#x27;vue-i18n&#x27;</span></span><br><span class="line"><span class="keyword">import</span> enLocale <span class="keyword">from</span> <span class="string">&#x27;element-ui/lib/locale/lang/en&#x27;</span></span><br><span class="line"><span class="keyword">import</span> zhCNLocale <span class="keyword">from</span> <span class="string">&#x27;element-ui/lib/locale/lang/zh-CN&#x27;</span></span><br><span class="line"><span class="keyword">import</span> zhTWLocale <span class="keyword">from</span> <span class="string">&#x27;element-ui/lib/locale/lang/zh-TW&#x27;</span></span><br><span class="line"></span><br><span class="line">Vue.use(VueI18n)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> langFiles = <span class="built_in">require</span>.context(<span class="string">&#x27;./config&#x27;</span>, <span class="literal">false</span>, <span class="regexp">/\.js$/</span>)</span><br><span class="line"><span class="keyword">const</span> regExp = <span class="regexp">/\.\/([^\.\/]+)\.([^\.]+)$/</span></span><br><span class="line"><span class="keyword">const</span> messages = &#123;&#125;</span><br><span class="line"><span class="keyword">const</span> lang = &#123;</span><br><span class="line">  zh: zhCNLocale,</span><br><span class="line">  zhTW: zhTWLocale,</span><br><span class="line">  en: enLocale</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 默认语言</span></span><br><span class="line"><span class="keyword">const</span> loadLanguage = <span class="string">&#x27;zh&#x27;</span></span><br><span class="line"></span><br><span class="line">langFiles.keys().forEach(<span class="function"><span class="params">key</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> k = regExp.exec(key)[<span class="number">1</span>]</span><br><span class="line">  <span class="comment">// 合并Element国际化配置</span></span><br><span class="line">  messages[k] = <span class="built_in">Object</span>.assign(langFiles(key).default, lang[k])</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getLanguage</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 第一次进入页面或手动清除设置默认语言</span></span><br><span class="line">  <span class="built_in">localStorage</span>.getItem(<span class="string">&#x27;lang&#x27;</span>) ? <span class="literal">null</span> : <span class="built_in">localStorage</span>.setItem(<span class="string">&#x27;lang&#x27;</span>, loadLanguage)</span><br><span class="line">  <span class="keyword">let</span> locale = <span class="built_in">localStorage</span>.getItem(<span class="string">&#x27;lang&#x27;</span>)</span><br><span class="line">  <span class="keyword">if</span> (!(locale <span class="keyword">in</span> messages)) locale = loadLanguage</span><br><span class="line">  <span class="keyword">return</span> locale</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> i18n = <span class="keyword">new</span> VueI18n(&#123;</span><br><span class="line">  locale: getLanguage(),</span><br><span class="line">  messages</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> i18n</span><br></pre></td></tr></table></figure>

<p>到此为止，Element 和 页面国际化基本做完了，不过还有一些国际化难点，接下来说一下</p>
<h2 id="3-与vuex进行关联"><a href="#3-与vuex进行关联" class="headerlink" title="3.与vuex进行关联"></a>3.与vuex进行关联</h2><blockquote>
<p>参考：<a target="_blank" rel="noopener" href="https://github.com/vueComponent/ant-design-vue-pro">ant-design-vue-pro</a></p>
</blockquote>
<ul>
<li>增加给 HTML 根节点增加 <code>lang</code> 属性</li>
<li>可以在 <code>loadLanguageAsync</code> 力作缓存语言设置，切换语言时懒加载对应语言，以提升性能</li>
<li>增加 <code>i18nRender</code> 方法</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* i18n/index.js */</span></span><br><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> VueI18n <span class="keyword">from</span> <span class="string">&#x27;vue-i18n&#x27;</span></span><br><span class="line"><span class="keyword">import</span> enLocale <span class="keyword">from</span> <span class="string">&#x27;element-ui/lib/locale/lang/en&#x27;</span></span><br><span class="line"><span class="keyword">import</span> zhCNLocale <span class="keyword">from</span> <span class="string">&#x27;element-ui/lib/locale/lang/zh-CN&#x27;</span></span><br><span class="line"><span class="keyword">import</span> zhTWLocale <span class="keyword">from</span> <span class="string">&#x27;element-ui/lib/locale/lang/zh-TW&#x27;</span></span><br><span class="line"></span><br><span class="line">Vue.use(VueI18n)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> langFiles = <span class="built_in">require</span>.context(<span class="string">&#x27;./config&#x27;</span>, <span class="literal">false</span>, <span class="regexp">/\.js$/</span>)</span><br><span class="line"><span class="keyword">const</span> regExp = <span class="regexp">/\.\/([^\.\/]+)\.([^\.]+)$/</span></span><br><span class="line"><span class="keyword">const</span> messages = &#123;&#125;</span><br><span class="line"><span class="keyword">const</span> lang = &#123;</span><br><span class="line">  zh: zhCNLocale,</span><br><span class="line">  zhTW: zhTWLocale,</span><br><span class="line">  en: enLocale</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 默认语言</span></span><br><span class="line"><span class="keyword">const</span> loadLanguage = <span class="string">&#x27;zh&#x27;</span></span><br><span class="line"></span><br><span class="line">langFiles.keys().forEach(<span class="function"><span class="params">key</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> k = regExp.exec(key)[<span class="number">1</span>]</span><br><span class="line">  <span class="comment">// 合并Element国际化配置</span></span><br><span class="line">  messages[k] = <span class="built_in">Object</span>.assign(langFiles(key).default, lang[k])</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">setI18nLanguage</span>(<span class="params">lang</span>) </span>&#123;</span><br><span class="line">  i18n.locale = lang</span><br><span class="line">  <span class="built_in">localStorage</span>.setItem(<span class="string">&#x27;lang&#x27;</span>, lang)</span><br><span class="line">  <span class="built_in">document</span>.querySelector(<span class="string">&#x27;html&#x27;</span>).setAttribute(<span class="string">&#x27;lang&#x27;</span>, lang)</span><br><span class="line">  <span class="keyword">return</span> lang</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> i18n = <span class="keyword">new</span> VueI18n(&#123;</span><br><span class="line">  silentTranslationWarn: <span class="literal">true</span>, <span class="comment">// 取消本地化失败时输出的警告</span></span><br><span class="line">  locale: getLanguage(), <span class="comment">// 语言环境</span></span><br><span class="line">  messages</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">getLanguage</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 第一次进入页面或手动清除设置默认语言</span></span><br><span class="line">  <span class="built_in">localStorage</span>.getItem(<span class="string">&#x27;lang&#x27;</span>) ? <span class="literal">null</span> : <span class="built_in">localStorage</span>.setItem(<span class="string">&#x27;lang&#x27;</span>, loadLanguage)</span><br><span class="line">  <span class="keyword">let</span> locale = <span class="built_in">localStorage</span>.getItem(<span class="string">&#x27;lang&#x27;</span>)</span><br><span class="line">  <span class="keyword">if</span> (!(locale <span class="keyword">in</span> messages)) locale = loadLanguage</span><br><span class="line">  <span class="keyword">return</span> locale</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 可以在这里做动态加载国际化语言</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">loadLanguageAsync</span>(<span class="params">lang</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="params">resolve</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> resolve(setI18nLanguage(lang))</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">i18nRender</span>(<span class="params">key</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> i18n.t(<span class="string">`<span class="subst">$&#123;key&#125;</span>`</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> i18n</span><br></pre></td></tr></table></figure>

<p>在 vuex 里增加 <code>state</code>、<code>mutation</code>、<code>actions</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* store/modules/app.js */</span></span><br><span class="line"><span class="keyword">import</span> &#123; loadLanguageAsync &#125; <span class="keyword">from</span> <span class="string">&#x27;@/i18n&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> state = &#123;</span><br><span class="line">  lang: <span class="string">&#x27;zh&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> mutations = &#123;</span><br><span class="line">  SET_LANG: <span class="function">(<span class="params">state, lang</span>) =&gt;</span> &#123;</span><br><span class="line">    state.lang = lang</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> actions = &#123;</span><br><span class="line">  <span class="function"><span class="title">setLang</span>(<span class="params">&#123; commit &#125;, lang</span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">      commit(<span class="string">&#x27;SET_LANG&#x27;</span>, lang)</span><br><span class="line">      loadLanguageAsync(lang)</span><br><span class="line">        .then(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">          resolve()</span><br><span class="line">        &#125;)</span><br><span class="line">        .catch(<span class="function"><span class="params">e</span> =&gt;</span> &#123;</span><br><span class="line">          reject(e)</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在 vuex 的 <code>getters</code> 里增加 lang</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* store/getters.js */</span></span><br><span class="line"><span class="keyword">const</span> getters = &#123;</span><br><span class="line">  lang: <span class="function"><span class="params">state</span> =&gt;</span> state.app.lang</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> getters</span><br></pre></td></tr></table></figure>

<p>在创建 Vue 实例时 <code>dispatch</code> 让 <code>state</code> 有 lang 的状态</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* main.js */</span></span><br><span class="line"><span class="keyword">import</span> App <span class="keyword">from</span> <span class="string">&#x27;./App&#x27;</span></span><br><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">&#x27;@/store&#x27;</span></span><br><span class="line"><span class="keyword">import</span> i18n <span class="keyword">from</span> <span class="string">&#x27;@/i18n&#x27;</span> </span><br><span class="line"><span class="keyword">import</span> &#123; getLanguage &#125; <span class="keyword">from</span> <span class="string">&#x27;@/i18n&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">createdConfig</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  store.dispatch(<span class="string">&#x27;app/setLang&#x27;</span>, getLanguage())</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Vue(&#123;</span><br><span class="line">  el: <span class="string">&#x27;#app&#x27;</span>,</span><br><span class="line">  store,</span><br><span class="line">  i18n,</span><br><span class="line">  created: createdConfig,</span><br><span class="line">  render: <span class="function"><span class="params">h</span> =&gt;</span> h(App)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>可以将修改语言抽成组件（这里只写 JS 部分）</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; mapState &#125; <span class="keyword">from</span> <span class="string">&#x27;vuex&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  computed: &#123;</span><br><span class="line">    ...mapState(&#123;</span><br><span class="line">      language: <span class="function"><span class="params">state</span> =&gt;</span> state.app.lang</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line">    <span class="function"><span class="title">handleSetLanguage</span>(<span class="params">lang</span>)</span> &#123;</span><br><span class="line">      <span class="built_in">this</span>.$store.dispatch(<span class="string">&#x27;app/setLang&#x27;</span>, lang).then(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">window</span>.location.reload()</span><br><span class="line">      &#125;).catch(<span class="function">() =&gt;</span> &#123;&#125;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="路由导航篇"><a href="#路由导航篇" class="headerlink" title="路由导航篇"></a>路由导航篇</h1><p>这里有一些前端后端都可以处理，如果后端进行国际化处理，前端需要在请求拦截器里加 <code>Accept-Language</code> 请求头</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> axios <span class="keyword">from</span> <span class="string">&#x27;axios&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> service = axios.create(&#123;</span><br><span class="line">  baseURL: process.env.VUE_APP_BASE_API,</span><br><span class="line">  withCredentials: <span class="literal">true</span>,</span><br><span class="line">  timeout: <span class="number">5000</span></span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">service.interceptors.request.use(</span><br><span class="line">  config =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> langConfig = &#123;</span><br><span class="line">      zh: <span class="string">&#x27;zh_CN&#x27;</span>,</span><br><span class="line">      en: <span class="string">&#x27;en_US&#x27;</span>,</span><br><span class="line">      zhTW: <span class="string">&#x27;zh_TW&#x27;</span>,</span><br><span class="line">      other: <span class="string">&#x27;other&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">    config.headers[<span class="string">&#x27;Accept-Language&#x27;</span>] = langConfig[locale]</span><br><span class="line">    <span class="keyword">return</span> config</span><br><span class="line">  &#125;,</span><br><span class="line">  error =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">Promise</span>.reject(error)</span><br><span class="line">  &#125;</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> service</span><br></pre></td></tr></table></figure>

<h2 id="1-路由菜单国际化"><a href="#1-路由菜单国际化" class="headerlink" title="1.路由菜单国际化"></a>1.路由菜单国际化</h2><h3 id="使用-te-判断是否存在语言包内"><a href="#使用-te-判断是否存在语言包内" class="headerlink" title="* 使用 $te 判断是否存在语言包内"></a>* 使用 $te 判断是否存在语言包内</h3><ul>
<li><p>首先需要处理一下菜单名，如果这个名在语言配置文件中就直接返回，如果不在返回原信息中的菜单。这个可以后端进行国际化处理，如果想修改 <code>UPDATE</code> 数据库一下即可</p>
<p><code>$te</code> 这个方法用以判断需要翻译的 <code>key</code> 在你提供的 <code>语言包(messages)</code> 中是否存在</p>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* utils/get-page-title.js */</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">routeTitle</span>(<span class="params">item</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> name = <span class="string">`sidebar.<span class="subst">$&#123;item.path&#125;</span>`</span></span><br><span class="line">  <span class="keyword">if</span> (<span class="built_in">this</span>.$te(name)) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.$t(name)</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> item.meta.title</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="展示页面-1"><a href="#展示页面-1" class="headerlink" title="展示页面"></a>展示页面</h3><p>把每一项传到处理菜单的方法中，这样路由菜单国际化基本完成了，如果有多级路由，基本同理</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span> <span class="attr">v-for</span>=<span class="string">&quot;(item, index) in routesSystem&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">el-menu-item</span> <span class="attr">:index</span>=<span class="string">&quot;index.toString()&quot;</span>&gt;</span>&#123;&#123; routeTitle(item) &#125;&#125;<span class="tag">&lt;/<span class="name">el-menu-item</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; routeTitle &#125; <span class="keyword">from</span> <span class="string">&#x27;@/utils/get-page-title&#x27;</span></span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line">  methods: &#123;</span><br><span class="line"><span class="javascript">    routeTitle <span class="comment">// 声明一下</span></span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="2-面包屑导航国际化"><a href="#2-面包屑导航国际化" class="headerlink" title="2.面包屑导航国际化"></a>2.面包屑导航国际化</h2><p>面包屑导航使用的是 <code>vue-element-admin</code> 的 Breadcrumb 组件</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">el-breadcrumb</span> <span class="attr">class</span>=<span class="string">&quot;app-breadcrumb&quot;</span> <span class="attr">separator</span>=<span class="string">&quot;/&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">transition-group</span> <span class="attr">name</span>=<span class="string">&quot;breadcrumb&quot;</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">el-breadcrumb-item</span> <span class="attr">v-for</span>=<span class="string">&quot;(item, index) in levelList&quot;</span> <span class="attr">:key</span>=<span class="string">&quot;item.path&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">span</span> <span class="attr">v-if</span>=<span class="string">&quot;item.redirect === &#x27;noRedirect&#x27; || index == levelList.length - 1&quot;</span> <span class="attr">class</span>=<span class="string">&quot;no-redirect&quot;</span>&gt;</span>&#123;&#123; breadTitle(item) &#125;&#125;<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">a</span> <span class="attr">v-else</span>&gt;</span>&#123;&#123; breadTitle(item) &#125;&#125;<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">el-breadcrumb-item</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">transition-group</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">el-breadcrumb</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; breadTitle &#125; <span class="keyword">from</span> <span class="string">&#x27;@/utils/get-page-title&#x27;</span></span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line">  methods: &#123;</span><br><span class="line"><span class="javascript">    breadTitle <span class="comment">// 声明一下</span></span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>可以通过 <code>/</code> 去判断它是几级路由；也可以在路由的 <code>meta</code> 中设定一个标识，判断这个标识即可</p>
<ul>
<li>三级路由场景：一、二级路由可以放在 <code>sidebar</code> 对象中，三级路由放在 <code>menu</code> 中（当然也可以都放在一个对象里）</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* utils/get-page-title.js */</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">breadTitle</span>(<span class="params">item</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (<span class="built_in">window</span>.indexConfig.afterTitle) <span class="keyword">return</span> item.meta.title</span><br><span class="line">  <span class="keyword">const</span> n = item.name || item.path.slice(<span class="number">1</span>)</span><br><span class="line">  <span class="keyword">let</span> name</span><br><span class="line">  <span class="keyword">if</span> (item.path.match(<span class="regexp">/\//g</span>).length !== <span class="number">3</span>) &#123;</span><br><span class="line">    name = <span class="string">`sidebar.<span class="subst">$&#123;n&#125;</span>`</span></span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    name = <span class="string">`menu.<span class="subst">$&#123;n&#125;</span>`</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">if</span> (<span class="built_in">this</span>.$te(name)) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.$t(name)</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> item.meta.title</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="3-导航守卫国际化"><a href="#3-导航守卫国际化" class="headerlink" title="3.导航守卫国际化"></a>3.导航守卫国际化</h2><p>因为导航守卫中拿不到 <code>this</code>，所以可以采用了一种取巧的方式。首先在 <code>router</code> 文件中引入 <code>i18n</code> 文件夹下的 <code>index.js</code>，之后直接取它的 <code>messages</code> 和 <code>locale</code> 即可拿到对应语言文件</p>
<ul>
<li>不过这样写会产生一个问题，就是切换语言无法实时更新，需要刷新状态</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* src/permission.js */</span></span><br><span class="line"><span class="keyword">import</span> router <span class="keyword">from</span> <span class="string">&#x27;./router/index&#x27;</span></span><br><span class="line"><span class="keyword">import</span> i18n <span class="keyword">from</span> <span class="string">&#x27;@jp/i18n&#x27;</span></span><br><span class="line"><span class="keyword">import</span> modal <span class="keyword">from</span> <span class="string">&#x27;@jp/utils/modal&#x27;</span> <span class="comment">// 二次封装的弹出框</span></span><br><span class="line"><span class="keyword">import</span> NProgress <span class="keyword">from</span> <span class="string">&#x27;nprogress&#x27;</span></span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;nprogress/nprogress.css&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123; getToken, removeCookies, removeToken &#125; <span class="keyword">from</span> <span class="string">&#x27;@/utils/auth&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123; isDebugger &#125; <span class="keyword">from</span> <span class="string">&#x27;@/settings&#x27;</span> <span class="comment">// 也可以维护在store里</span></span><br><span class="line"></span><br><span class="line">NProgress.configure(&#123; <span class="attr">showSpinner</span>: <span class="literal">false</span> &#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> locale = i18n.locale</span><br><span class="line"><span class="keyword">const</span> msg = i18n.messages[locale]</span><br><span class="line"><span class="keyword">const</span> whiteList = [<span class="string">&#x27;/login&#x27;</span>]</span><br><span class="line"></span><br><span class="line">router.beforeEach(<span class="function">(<span class="params">to, <span class="keyword">from</span>, next</span>) =&gt;</span> &#123;</span><br><span class="line">  NProgress.start()</span><br><span class="line">  <span class="keyword">if</span> (getToken()) &#123;</span><br><span class="line">    <span class="built_in">document</span>.title = getPageTitle(to.meta.title)</span><br><span class="line">    <span class="keyword">if</span> (to.path === <span class="string">&#x27;/login&#x27;</span>) &#123;</span><br><span class="line">      next(&#123; <span class="attr">path</span>: <span class="string">&#x27;/&#x27;</span> &#125;)</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="comment">// debugger下不获取用户权限</span></span><br><span class="line">      <span class="keyword">if</span> (isDebugger) &#123;</span><br><span class="line">        next()</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 设置动态路由，拉取user_info...</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (whiteList.includes(to.path)) &#123;</span><br><span class="line">      next()</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      removeCookies()</span><br><span class="line">      modal.msgError(msg.info.tokenExpire) <span class="comment">// 弹出框国际化</span></span><br><span class="line">      next(&#123; <span class="attr">path</span>: <span class="string">&#x27;/login&#x27;</span> &#125;)</span><br><span class="line">      NProgress.done()</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">router.afterEach(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">  NProgress.done()</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h2 id="4-render-函数展示文字"><a href="#4-render-函数展示文字" class="headerlink" title="4.render 函数展示文字"></a>4.render 函数展示文字</h2><p> <code>vue-element-admin</code> 里的 Item 组件，是采用 <code>render</code> 函数渲染的，这个一般</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- src/layout/components/Sidebar/Item.vue --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; childTitle &#125; <span class="keyword">from</span> <span class="string">&#x27;@/utils/get-page-title&#x27;</span></span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line"><span class="javascript">  name: <span class="string">&#x27;MenuItem&#x27;</span>,</span></span><br><span class="line"><span class="javascript">  functional: <span class="literal">true</span>,</span></span><br><span class="line">  props: &#123;</span><br><span class="line">    icon: &#123;</span><br><span class="line"><span class="javascript">      type: <span class="built_in">String</span>,</span></span><br><span class="line"><span class="javascript">      <span class="keyword">default</span>: <span class="string">&#x27;&#x27;</span></span></span><br><span class="line">    &#125;,</span><br><span class="line">    title: &#123;</span><br><span class="line"><span class="javascript">      type: <span class="built_in">String</span>,</span></span><br><span class="line"><span class="javascript">      <span class="keyword">default</span>: <span class="string">&#x27;&#x27;</span></span></span><br><span class="line">    &#125;,</span><br><span class="line">    path: &#123;</span><br><span class="line"><span class="javascript">      type: <span class="built_in">String</span>,</span></span><br><span class="line"><span class="javascript">      <span class="keyword">default</span>: <span class="string">&#x27;&#x27;</span></span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line"><span class="javascript">  <span class="function"><span class="title">render</span>(<span class="params">h, context</span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">const</span> &#123; icon, title &#125; = context.props</span></span><br><span class="line"><span class="javascript">    <span class="keyword">const</span> vnodes = []</span></span><br><span class="line"></span><br><span class="line"><span class="javascript">    <span class="keyword">if</span> (icon) &#123;</span></span><br><span class="line"><span class="javascript">      <span class="keyword">if</span> (icon.includes(<span class="string">&#x27;el-icon&#x27;</span>)) &#123;</span></span><br><span class="line"><span class="javascript">        vnodes.push(<span class="xml"><span class="tag">&lt;<span class="name">i</span> <span class="attr">class</span>=<span class="string">&#123;[icon,</span> &#x27;<span class="attr">sub-el-icon</span>&#x27;]&#125; /&gt;</span></span>)</span></span><br><span class="line"><span class="javascript">      &#125; <span class="keyword">else</span> &#123;</span></span><br><span class="line"><span class="javascript">        vnodes.push(<span class="xml"><span class="tag">&lt;<span class="name">svg-icon</span> <span class="attr">icon-class</span>=<span class="string">&#123;icon&#125;/</span>&gt;</span></span>)</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="javascript">    <span class="keyword">if</span> (title) &#123;</span></span><br><span class="line"><span class="javascript">      <span class="comment">// 三级目录文字展示</span></span></span><br><span class="line"><span class="javascript">      vnodes.push(<span class="xml"><span class="tag">&lt;<span class="name">span</span> <span class="attr">slot</span>=<span class="string">&quot;title&quot;</span>&gt;</span>&#123;childTitle(context)&#125;<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span>)</span></span><br><span class="line">    &#125;</span><br><span class="line"><span class="javascript">    <span class="keyword">return</span> vnodes</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>这里可以用一个取巧的方式，因为 <code>render</code> 函数里的 <code>context</code> 里面有 <code>parent</code> 父节点，可以使用这个父节点上面的方法（<code>$t</code>、<code>$te</code>）</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">childTitle</span>(<span class="params">item</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> p = item.props.path</span><br><span class="line">  <span class="keyword">const</span> parent = item.parent</span><br><span class="line">  <span class="keyword">const</span> name = <span class="string">`menu.<span class="subst">$&#123;p&#125;</span>`</span></span><br><span class="line">  <span class="keyword">if</span> (parent.$te(name)) &#123;</span><br><span class="line">    <span class="keyword">return</span> parent.$t(name)</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> item.props.title</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这样路由菜单、面包屑导航及导航守卫国际化基本做完事了</p>
<h1 id="组件篇"><a href="#组件篇" class="headerlink" title="组件篇"></a>组件篇</h1><h2 id="1-选择框、输入框组件"><a href="#1-选择框、输入框组件" class="headerlink" title="1.选择框、输入框组件"></a>1.选择框、输入框组件</h2><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">el-form-item</span> <span class="attr">:label</span>=<span class="string">&quot;$t(&#x27;viewsC.animal&#x27;)&quot;</span> <span class="attr">prop</span>=<span class="string">&quot;animal&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">el-input</span> <span class="attr">v-model</span>=<span class="string">&quot;rowsFormData.animal&quot;</span> <span class="attr">:placeholder</span>=<span class="string">&quot;$t(&#x27;viewsC.animalHolder&#x27;)&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">el-form-item</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>选择框输入框，我将其都放在 viewC 对象中，并按照，如下方式命名：</p>
<ul>
<li>没有加 Holder 后缀的后面需要加 <code>：</code>，有加 Holder 后缀的前面需要加 <code>请输入/请选择</code></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> ps = <span class="string">&#x27;请选择&#x27;</span></span><br><span class="line"><span class="keyword">const</span> pi = <span class="string">&#x27;请输入&#x27;</span></span><br><span class="line"><span class="keyword">const</span> end = <span class="string">&#x27;：&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> viewsC = &#123;</span><br><span class="line">  startDate: <span class="string">&#x27;起始日期&#x27;</span>,</span><br><span class="line">  startDateHolder: ps + <span class="string">&#x27;起始日期&#x27;</span>,</span><br><span class="line">  stopDate: <span class="string">&#x27;截止日期&#x27;</span>,</span><br><span class="line">  stopDateHolder: ps + <span class="string">&#x27;截止日期&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">const</span> item <span class="keyword">in</span> viewsC) &#123;</span><br><span class="line">  <span class="keyword">if</span> (!item.includes(<span class="string">&#x27;Holder&#x27;</span>)) &#123;</span><br><span class="line">    viewsC[item] = viewsC[item] + end</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (!viewsC[item].includes(<span class="string">&#x27;请&#x27;</span>)) &#123;</span><br><span class="line">      <span class="built_in">console</span>.log(viewsC[item] + <span class="string">&#x27;需要加 ps 或 pi&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>详细例子可以参考 <code>2.选择框、输入框表单校验</code>，表格 Table 跟这个同理 <code>el-table-column : label=&quot;$t(&#39;tableC.xxx)&quot;</code> 即可</p>
<h2 id="2-选择框、输入框表单校验"><a href="#2-选择框、输入框表单校验" class="headerlink" title="* 2.选择框、输入框表单校验"></a>* 2.选择框、输入框表单校验</h2><p>如果直接把 <code>rules</code> 校验规则写在 <code>data</code> 里面，Element 内部对其 <code>this</code> 进行了处理，所以需要换一种思路</p>
<ol>
<li><p>使用 <code>computed</code> 计算属性</p>
</li>
<li><p>写在 <code>data</code> 里面，在 return 之前是可以拿到 <code>this</code> 的，<code>message</code> 取到这个变量即可（不过这个切换语言不能实时更新）</p>
<p>注意：如果使用自定义规则是可以实时更新的，但是出于复用性一般会将其抽到 utils 方法库中，这样就会产生其他问题</p>
<ul>
<li><code>this</code> 无法处理</li>
<li>简单认证还得写复杂的自定义规则</li>
</ul>
<p>所以这个不是一个好方法，推荐使用计算属性</p>
</li>
</ol>
<p><strong>一般出于复用性将方法抽到 utils 方法库中，直接写是不行的</strong>（Element 内部对其 <code>this</code> 进行了处理），需要结合 <code>bind/call/apply</code> 来使用</p>
<ul>
<li><code>pattern: &#39;[^ \x22]+&#39;</code> 是校验不能全部为空格</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">el-scrollbar</span> <span class="attr">wrap-class</span>=<span class="string">&quot;scrollbar-wrapper&quot;</span> <span class="attr">style</span>=<span class="string">&quot;height: 100%;&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">el-form</span> <span class="attr">ref</span>=<span class="string">&quot;rowsForm&quot;</span> <span class="attr">:model</span>=<span class="string">&quot;rowsFormData&quot;</span> <span class="attr">:rules</span>=<span class="string">&quot;rules&quot;</span> <span class="attr">label-width</span>=<span class="string">&quot;130px&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">el-row</span> <span class="attr">:gutter</span>=<span class="string">&quot;20&quot;</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">el-col</span> <span class="attr">:span</span>=<span class="string">&quot;8&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">el-form-item</span> <span class="attr">:label</span>=<span class="string">&quot;$t(&#x27;viewsC.animal&#x27;)&quot;</span> <span class="attr">prop</span>=<span class="string">&quot;animal&quot;</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">el-input</span> <span class="attr">v-model</span>=<span class="string">&quot;rowsFormData.animal&quot;</span> <span class="attr">:placeholder</span>=<span class="string">&quot;$t(&#x27;viewsC.animalHolder&#x27;)&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">el-form-item</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">el-col</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">el-col</span> <span class="attr">:span</span>=<span class="string">&quot;8&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">el-form-item</span> <span class="attr">:label</span>=<span class="string">&quot;$t(&#x27;viewsC.plants&#x27;)&quot;</span> <span class="attr">prop</span>=<span class="string">&quot;plants&quot;</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">el-input</span> <span class="attr">v-model</span>=<span class="string">&quot;rowsFormData.plants&quot;</span> <span class="attr">:placeholder</span>=<span class="string">&quot;$t(&#x27;viewsC.plantsHolder&#x27;)&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">el-form-item</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">el-col</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">el-row</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">el-form</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">el-scrollbar</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; ValidAnimal &#125; <span class="keyword">from</span> <span class="string">&#x27;@/utils/viewValidate&#x27;</span></span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line"><span class="javascript">  <span class="function"><span class="title">data</span>(<span class="params"></span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">return</span> &#123;</span></span><br><span class="line">      rowsFormData: &#123;</span><br><span class="line"><span class="javascript">        animal: <span class="string">&#x27;&#x27;</span>,</span></span><br><span class="line"><span class="javascript">        plants: <span class="string">&#x27;&#x27;</span></span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  computed: &#123;</span><br><span class="line"><span class="javascript">    <span class="function"><span class="title">rules</span>(<span class="params"></span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">      <span class="keyword">return</span> &#123;</span></span><br><span class="line"><span class="javascript">        animal: [&#123; <span class="attr">required</span>: <span class="literal">true</span>, <span class="attr">validator</span>: ValidAnimal.bind(<span class="built_in">this</span>), <span class="attr">trigger</span>: <span class="string">&#x27;change&#x27;</span>, <span class="attr">pattern</span>: <span class="string">&#x27;[^ \x22]+&#x27;</span> &#125;],</span></span><br><span class="line">        plants: [</span><br><span class="line"><span class="javascript">          &#123; <span class="attr">required</span>: <span class="literal">true</span>, <span class="attr">message</span>: <span class="built_in">this</span>.$t(<span class="string">&#x27;form.plantsTip&#x27;</span>), <span class="attr">trigger</span>: <span class="string">&#x27;blur&#x27;</span>, <span class="attr">pattern</span>: <span class="string">&#x27;[^ \x22]+&#x27;</span> &#125;,</span></span><br><span class="line"><span class="javascript">          &#123; <span class="attr">max</span>: <span class="number">5</span>, <span class="attr">message</span>: <span class="built_in">this</span>.$t(<span class="string">&#x27;form.maxLength10&#x27;</span>), <span class="attr">trigger</span>: <span class="string">&#x27;blur&#x27;</span> &#125;</span></span><br><span class="line">        ]</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="3-弹出框（MessageBox-或-Notification）"><a href="#3-弹出框（MessageBox-或-Notification）" class="headerlink" title="3.弹出框（MessageBox 或 Notification）"></a>3.弹出框（MessageBox 或 Notification）</h2><p>弹出框的 <code>title</code> 是可以使用 <code>this</code> 的，不过需要注意：</p>
<ul>
<li><p>如果使用 function，拿到的是错误的 <code>this</code> （可能由于代码不规范导致有这个问题）</p>
<p><strong>要么用一个变量存，要么使用箭头函数</strong></p>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">this</span>.$confirm(<span class="built_in">this</span>.$t(<span class="string">&#x27;info.notify&#x27;</span>) , <span class="built_in">this</span>.$t(<span class="string">&#x27;info.friendlyTips&#x27;</span>), &#123;</span><br><span class="line">  type: <span class="string">&#x27;warning&#x27;</span></span><br><span class="line">&#125;)</span><br><span class="line">  .then(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">const</span> params = &#123;&#125;</span><br><span class="line">    Api(params)</span><br><span class="line">      .then(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.$notify(&#123; <span class="attr">title</span>: <span class="built_in">this</span>.$t(<span class="string">&#x27;info.friendlyTips&#x27;</span>), <span class="attr">message</span>: res.data.msg, <span class="attr">type</span>: <span class="string">&#x27;success&#x27;</span> &#125;)</span><br><span class="line">        <span class="built_in">this</span>.getList()</span><br><span class="line">      &#125;)</span><br><span class="line">      .catch(<span class="function"><span class="params">e</span> =&gt;</span> <span class="built_in">console</span>.log(e))</span><br><span class="line">  &#125;)</span><br></pre></td></tr></table></figure>

<h2 id="4-自制弹出框组件国际化（Vue-extend）"><a href="#4-自制弹出框组件国际化（Vue-extend）" class="headerlink" title="4.自制弹出框组件国际化（Vue.extend）"></a>4.自制弹出框组件国际化（Vue.extend）</h2><p><code>Vue.extend</code> 其实相当于创建了一个 <code>Vue</code> 子类，然后对这个子类上扩展一些 <code>options</code> 属性</p>
<h3 id="popup-js-中处理"><a href="#popup-js-中处理" class="headerlink" title="popup.js 中处理"></a>popup.js 中处理</h3><p>结合 <code>el-dialog</code> 自制弹出框组件，需要在创建实例时混入 <code>$i18n</code> ，这样弹出框组件才能使用 <code>$t</code>、<code>$te</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">&#x27;@/store&#x27;</span></span><br><span class="line"><span class="keyword">import</span> i18n <span class="keyword">from</span> <span class="string">&#x27;@/i18n&#x27;</span> <span class="comment">// 引入国际化</span></span><br><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="function"><span class="keyword">function</span>(<span class="params">el, params</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 初始化一个Promise</span></span><br><span class="line">  <span class="keyword">let</span> resolve</span><br><span class="line">  <span class="keyword">let</span> reject</span><br><span class="line">  <span class="keyword">const</span> promise = <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">res, rej</span>) =&gt;</span> &#123;</span><br><span class="line">    resolve = res</span><br><span class="line">    reject = rej</span><br><span class="line">  &#125;)</span><br><span class="line">  <span class="comment">// 初始化弹窗</span></span><br><span class="line">  <span class="keyword">const</span> Popups = Vue.extend(el)</span><br><span class="line">  <span class="keyword">const</span> vEl = <span class="keyword">new</span> Popups(&#123;</span><br><span class="line">    store,</span><br><span class="line">    i18n,</span><br><span class="line">    propsData: &#123;</span><br><span class="line">      params: <span class="built_in">Object</span>.assign(&#123;&#125;, params)</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="function"><span class="title">data</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> &#123;</span><br><span class="line">        visible: <span class="literal">true</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    methods: &#123;</span><br><span class="line">      <span class="function"><span class="title">resolve</span>(<span class="params">arg</span>)</span> &#123;</span><br><span class="line">        resolve(arg)</span><br><span class="line">        <span class="built_in">this</span>.visible = <span class="literal">false</span></span><br><span class="line">      &#125;,</span><br><span class="line">      <span class="function"><span class="title">reject</span>(<span class="params">arg</span>)</span> &#123;</span><br><span class="line">        reject(arg)</span><br><span class="line">        <span class="built_in">this</span>.visible = <span class="literal">false</span></span><br><span class="line">      &#125;,</span><br><span class="line">      <span class="function"><span class="title">closed</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.visible = <span class="literal">false</span></span><br><span class="line">        removePopup()</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">  <span class="comment">// 把弹窗挂载到body</span></span><br><span class="line">  <span class="keyword">const</span> shadeEl = vEl.$mount().$el</span><br><span class="line">  <span class="built_in">document</span>.body.appendChild(shadeEl)</span><br><span class="line">  <span class="comment">// 从body中移除弹窗体</span></span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">removePopup</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">document</span>.body.removeChild(shadeEl)</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> promise</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="popup-vue-组件"><a href="#popup-vue-组件" class="headerlink" title="popup.vue 组件"></a>popup.vue 组件</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  &lt;el-dialog</span><br><span class="line">    :title=&quot;title&quot;</span><br><span class="line">    :width=&quot;width&quot;</span><br><span class="line">    :visible.sync=&quot;visible&quot;</span><br><span class="line">    append-to-body</span><br><span class="line">    :close-on-click-modal=&quot;false&quot;</span><br><span class="line">    :close-on-press-escape=&quot;false&quot;</span><br><span class="line">    :show-close=&quot;showClose&quot;</span><br><span class="line">    :modal=&quot;modal&quot;</span><br><span class="line">    :custom-class=&quot;customClass&quot;</span><br><span class="line">    class=&quot;popup-wrapper&quot;</span><br><span class="line">    :top=&quot;top&quot;</span><br><span class="line">    @closed=&quot;closed&quot;</span><br><span class="line">  &gt;</span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;popup__body&quot;</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">slot</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">v-if</span>=<span class="string">&quot;!isFooter&quot;</span> <span class="attr">slot</span>=<span class="string">&quot;footer&quot;</span> <span class="attr">class</span>=<span class="string">&quot;dialog-footer&quot;</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">slot</span> <span class="attr">name</span>=<span class="string">&quot;footer&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">el-button</span> @<span class="attr">click</span>=<span class="string">&quot;onCancel&quot;</span>&gt;</span>&#123;&#123; $t(&#x27;tools.cancel&#x27;) &#125;&#125;<span class="tag">&lt;/<span class="name">el-button</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">el-button</span> <span class="attr">type</span>=<span class="string">&quot;primary&quot;</span> @<span class="attr">click</span>=<span class="string">&quot;onSubmit&quot;</span>&gt;</span>&#123;&#123; $t(&#x27;tools.confirm&#x27;) &#125;&#125;<span class="tag">&lt;/<span class="name">el-button</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">slot</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">el-dialog</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line"><span class="javascript">  name: <span class="string">&#x27;UiPopup&#x27;</span>,</span></span><br><span class="line">  props: &#123;</span><br><span class="line">    value: &#123;</span><br><span class="line"><span class="javascript">      type: [<span class="built_in">String</span>, <span class="built_in">Object</span>, <span class="built_in">Array</span>, <span class="built_in">Number</span>, <span class="built_in">Boolean</span>],</span></span><br><span class="line"><span class="javascript">      <span class="keyword">default</span>: <span class="string">&#x27;&#x27;</span></span></span><br><span class="line">    &#125;,</span><br><span class="line">    title: &#123;</span><br><span class="line"><span class="javascript">      type: <span class="built_in">String</span>,</span></span><br><span class="line"><span class="javascript">      <span class="keyword">default</span>: <span class="string">&#x27;&#x27;</span></span></span><br><span class="line">    &#125;,</span><br><span class="line">    width: &#123;</span><br><span class="line"><span class="javascript">      type: <span class="built_in">String</span>,</span></span><br><span class="line"><span class="javascript">      <span class="keyword">default</span>: <span class="string">&#x27;&#x27;</span></span></span><br><span class="line">    &#125;,</span><br><span class="line">    isFooter: &#123;</span><br><span class="line"><span class="javascript">      type: <span class="built_in">String</span>,</span></span><br><span class="line"><span class="javascript">      <span class="keyword">default</span>: <span class="string">&#x27;&#x27;</span></span></span><br><span class="line">    &#125;,</span><br><span class="line">    showClose: &#123;</span><br><span class="line"><span class="javascript">      type: <span class="built_in">Boolean</span>,</span></span><br><span class="line"><span class="javascript">      <span class="keyword">default</span>: <span class="literal">true</span></span></span><br><span class="line">    &#125;,</span><br><span class="line">    modal: &#123;</span><br><span class="line"><span class="javascript">      type: <span class="built_in">Boolean</span>,</span></span><br><span class="line"><span class="javascript">      <span class="keyword">default</span>: <span class="literal">true</span></span></span><br><span class="line">    &#125;,</span><br><span class="line">    customClass: &#123;</span><br><span class="line"><span class="javascript">      type: <span class="built_in">String</span>,</span></span><br><span class="line"><span class="javascript">      <span class="keyword">default</span>: <span class="string">&#x27;&#x27;</span></span></span><br><span class="line">    &#125;,</span><br><span class="line">    top: &#123;</span><br><span class="line"><span class="javascript">      type: <span class="built_in">String</span>,</span></span><br><span class="line"><span class="javascript">      <span class="keyword">default</span>: <span class="string">&#x27;&#x27;</span></span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  computed: &#123;</span><br><span class="line"><span class="javascript">    <span class="comment">// visible控制器，点X时调用取消</span></span></span><br><span class="line">    visible: &#123;</span><br><span class="line"><span class="javascript">      get: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">return</span> <span class="built_in">this</span>.$parent.visible</span></span><br><span class="line">      &#125;,</span><br><span class="line"><span class="javascript">      set: <span class="function"><span class="keyword">function</span>(<span class="params">v</span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="built_in">this</span>.$parent.reject(<span class="built_in">this</span>.value)</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line"><span class="javascript">    <span class="function"><span class="title">onCancel</span>(<span class="params"></span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">      <span class="built_in">this</span>.$emit(<span class="string">&#x27;onCancel&#x27;</span>, <span class="built_in">this</span>.value)</span></span><br><span class="line">    &#125;,</span><br><span class="line"><span class="javascript">    <span class="function"><span class="title">onSubmit</span>(<span class="params"></span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">      <span class="built_in">this</span>.$emit(<span class="string">&#x27;onSubmit&#x27;</span>, <span class="built_in">this</span>.value)</span></span><br><span class="line">    &#125;,</span><br><span class="line"><span class="javascript">    <span class="comment">// 点 X 关闭时调用父组件的注入</span></span></span><br><span class="line"><span class="javascript">    <span class="function"><span class="title">closed</span>(<span class="params"></span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">      <span class="built_in">this</span>.$parent.closed()</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="components-下的-index-js"><a href="#components-下的-index-js" class="headerlink" title="components 下的 index.js"></a>components 下的 index.js</h3><p>把 popup 通过全局方式注册</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> popup <span class="keyword">from</span> <span class="string">&#x27;./Popup/popup.vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> jsPopup <span class="keyword">from</span> <span class="string">&#x27;./Popup/popup.js&#x27;</span></span><br><span class="line"><span class="keyword">const</span> components = [</span><br><span class="line">  popup</span><br><span class="line">]</span><br><span class="line"><span class="keyword">const</span> jsComponents = &#123;</span><br><span class="line">  $popup: jsPopup</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 出口函数为组件安装函数</span></span><br><span class="line"><span class="keyword">const</span> install = <span class="function"><span class="params">Vue</span> =&gt;</span> &#123;</span><br><span class="line">  components.forEach(<span class="function"><span class="params">component</span> =&gt;</span> &#123;</span><br><span class="line">    Vue.component(component.name, component)</span><br><span class="line">  &#125;)</span><br><span class="line">  <span class="keyword">for</span> (<span class="keyword">const</span> key <span class="keyword">in</span> jsComponents) &#123;</span><br><span class="line">    <span class="keyword">if</span> (!Vue.prototype.hasOwnProperty(key)) &#123;</span><br><span class="line">      Vue.prototype[key] = jsComponents[key]</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="built_in">console</span>.warn(key + <span class="string">&#x27;被占用&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  install</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="main-js"><a href="#main-js" class="headerlink" title="main.js"></a>main.js</h3><p>这样就可以在全局使用 <code>$popup</code> 了</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> globalComponents <span class="keyword">from</span> <span class="string">&#x27;@/components&#x27;</span></span><br><span class="line"></span><br><span class="line">Vue.use(globalComponents)</span><br></pre></td></tr></table></figure>

<h1 id="打包篇"><a href="#打包篇" class="headerlink" title="打包篇"></a>打包篇</h1><p>toB 的项目可能会有这个需求：</p>
<ul>
<li>因为页面组件和表格很多，如果有一个字段名想改，就需要重新打包，很麻烦，现在想将其提出去（比如提取到 <code>public</code> 文件夹下的 config.js 文件中，这个 config.js 在 index.html 中引入一下），这样以后只需要改一下这个 js 文件无需重新打包</li>
</ul>
<h2 id="基础配置"><a href="#基础配置" class="headerlink" title="基础配置"></a>基础配置</h2><p>首先将各个配置文件复制到 <code>public</code> 下面，<strong>并将其用匿名函数包裹</strong>，之后在入口 index.html 中，引入如下配置文件</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">  <span class="keyword">var</span> now = <span class="keyword">new</span> <span class="built_in">Date</span>().getTime();</span></span><br><span class="line"><span class="javascript">  <span class="built_in">document</span>.write(<span class="string">&#x27;&lt;script src=&quot;../config.js?t=&#x27;</span> + now + <span class="string">&#x27;&quot;&gt;&lt;\/script\&gt;&#x27;</span>)</span></span><br><span class="line"><span class="javascript">  <span class="built_in">document</span>.write(<span class="string">&#x27;&lt;script src=&quot;../i18n/en.js?t=&#x27;</span> + now + <span class="string">&#x27;&quot;&gt;&lt;\/script\&gt;&#x27;</span>)</span></span><br><span class="line"><span class="javascript">  <span class="built_in">document</span>.write(<span class="string">&#x27;&lt;script src=&quot;../i18n/zh.js?t=&#x27;</span> + now + <span class="string">&#x27;&quot;&gt;&lt;\/script\&gt;&#x27;</span>)</span></span><br><span class="line"><span class="javascript">  <span class="built_in">document</span>.write(<span class="string">&#x27;&lt;script src=&quot;../i18n/zhTW.js?t=&#x27;</span> + now + <span class="string">&#x27;&quot;&gt;&lt;\/script\&gt;&#x27;</span>)</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>config.js 中用 <code>var</code> 声明变量挂载到 window 上，并在 <code>en/zh/zhTW</code> js文件中把配置加到 <code>lang</code> 对象中</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> indexConfig = &#123;&#125;</span><br><span class="line">indexConfig.lang = &#123;&#125;</span><br><span class="line"><span class="comment">// zh中文-简体； en-英文； zhTW-中文繁体</span></span><br><span class="line">indexConfig.langShow = [<span class="string">&#x27;zh&#x27;</span>, <span class="string">&#x27;en&#x27;</span>, <span class="string">&#x27;zhTW&#x27;</span>]</span><br></pre></td></tr></table></figure>

<p>最后在各个 <code>lang</code> 文件下用 <code>window.indexConfig.lang</code> 获取即可</p>
<h2 id="打包配置"><a href="#打包配置" class="headerlink" title="打包配置"></a>打包配置</h2><p>由于打包时不会携带 <code>i18n</code> 文件夹下面的配置文件，所以需要借助 CopyWebpackPlugin 插件进行复制</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> copyWebpackPlugins = [<span class="string">&#x27;&#x27;</span>, <span class="string">&#x27;/i18n&#x27;</span>].map(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> CopyWebpackPlugin([</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="keyword">from</span>: <span class="string">`./public<span class="subst">$&#123;item&#125;</span>/*`</span>,</span><br><span class="line">      to: path.resolve(__dirname, <span class="string">`dist/`</span>),</span><br><span class="line">      <span class="function"><span class="title">transformPath</span>(<span class="params">targetPath, absolutePath</span>)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> targetPath.replace(<span class="string">`public`</span>, <span class="string">&#x27;&#x27;</span>)</span><br><span class="line">      &#125;,</span><br><span class="line">      toType: <span class="string">&#x27;dir&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">  ])</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    plugins: [</span><br><span class="line">      <span class="keyword">new</span> HtmlWebpackPlugin(<span class="string">&#x27;src/index.html&#x27;</span>)</span><br><span class="line">    ].concat(copyWebpackPlugins)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这样配置文件就提出去了，不过有安全风险，这个以后再说</p>
<h2 id="推荐参考"><a href="#推荐参考" class="headerlink" title="推荐参考"></a>推荐参考</h2><p><a target="_blank" rel="noopener" href="https://segmentfault.com/a/1190000008752459">前端国际化之Vue-i18n源码分析</a></p>
<p><a target="_blank" rel="noopener" href="https://segmentfault.com/a/1190000016445415">vue中如何使用i18n实现国际化</a></p>
<p><a target="_blank" rel="noopener" href="https://element.eleme.cn/#/zh-CN/component/i18n">Element国际化</a></p>
<p><a target="_blank" rel="noopener" href="https://www.jianshu.com/p/c49296f13a17">elementUI——locale，国际化方案</a></p>

    </div>

    
    
    

	<div>
		  
			
<div class="my_post_copyright clearfix">
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>
  
  <!-- JS库 sweetalert 可修改路径 -->
  <script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <article class="box-left">
    <img src="https://gitee.com/lilyn/pic/raw/master/logo.jpg">
  </article>
  <article class="box-right">
    <p><span>本文标题:</span><a href="/posts/82482c19/">Vue+Element 国际化(i18n)页面全覆盖（路由、表单检验、弹出框等）</a></p>
    <p><span>文章作者:</span><a href="/" title="访问 LiLyn 的个人博客">LiLyn</a></p>
    <p><span>原始链接:</span><a href="/posts/82482c19/" title="Vue+Element 国际化(i18n)页面全覆盖（路由、表单检验、弹出框等）">http://llmysnow.top/posts/82482c19/</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="http://llmysnow.top/posts/82482c19/"  aria-label="复制成功！"></i></span>
  </p>
    <p><span>许可协议:</span>转载请保留原文链接及作者。</p>  
  </article>
</div>
<script> 
    var clipboard = new Clipboard('.fa-clipboard');
    $(".fa-clipboard").click(function(){
      clipboard.on('success', function(){
        swal({   
          title: "",   
          text: '复制成功',
          icon: "success", 
          showConfirmButton: true
          });
    });
    });  
</script>

		  
	</div>

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Vue/" rel="tag"><i class="fa fa-tag"></i> Vue</a>
              <a href="/tags/Element/" rel="tag"><i class="fa fa-tag"></i> Element</a>
              <a href="/tags/i18n/" rel="tag"><i class="fa fa-tag"></i> i18n</a>
              <a href="/tags/%E5%88%87%E6%8D%A2%E8%AF%AD%E8%A8%80/" rel="tag"><i class="fa fa-tag"></i> 切换语言</a>
          </div>
		 
		<!--post-tags 彩色标签-->
		<script type="text/javascript">
		var tagsall=document.getElementsByClassName("post-tags")
		for (var i = tagsall.length - 1; i >= 0; i--){
			var tags=tagsall[i].getElementsByTagName("a");
			for (var j = tags.length - 1; j >= 0; j--) {
				var r=Math.floor(Math.random()*75+130);
				var g=Math.floor(Math.random()*75+100);
				var b=Math.floor(Math.random()*75+80);
				tags[j].style.background = "rgb("+r+","+g+","+b+")";
				tags[j].style.color = "#fff";
				tags[j].style.padding = "0 10px";
			}
		}                        
		</script>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/posts/379f43e/" rel="prev" title="Element 日期选择器清除问题（Axios get请求问题）">
      <i class="fa fa-chevron-left"></i> Element 日期选择器清除问题（Axios get请求问题）
    </a></div>
      <div class="post-nav-item">
    <a href="/posts/7e319c36/" rel="next" title="手撕React.createElement和ReactDOM.render">
      手撕React.createElement和ReactDOM.render <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%AE%89%E8%A3%85-i18n"><span class="nav-text">安装 i18n</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE%E7%AF%87"><span class="nav-text">基础配置篇</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E9%A1%B5%E9%9D%A2%E4%B8%AD%E4%BD%BF%E7%94%A8%E5%9B%BD%E9%99%85%E5%8C%96"><span class="nav-text">1.页面中使用国际化</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#i18n-%E6%96%87%E4%BB%B6%E5%A4%B9%E4%B8%8B%E7%9A%84-index-js-%E6%96%87%E4%BB%B6"><span class="nav-text">* i18n 文件夹下的 index.js 文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#main-js-%E5%BC%95%E5%85%A5"><span class="nav-text">main.js 引入</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B1%95%E7%A4%BA%E9%A1%B5%E9%9D%A2"><span class="nav-text">展示页面</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%AD%E8%A8%80%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="nav-text">语言配置文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%8B%B1%E6%96%87%E9%A6%96%E5%AD%97%E6%AF%8D%E5%A4%A7%E5%86%99"><span class="nav-text">英文首字母大写</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-Element-%E5%9B%BD%E9%99%85%E5%8C%96"><span class="nav-text">2.Element 国际化</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#main-js-%E5%BC%95%E5%85%A5-1"><span class="nav-text">main.js 引入</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#i18n-%E6%96%87%E4%BB%B6%E5%A4%B9%E4%B8%8B%E7%9A%84-index-js-%E6%96%87%E4%BB%B6-1"><span class="nav-text">* i18n 文件夹下的 index.js 文件</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E4%B8%8Evuex%E8%BF%9B%E8%A1%8C%E5%85%B3%E8%81%94"><span class="nav-text">3.与vuex进行关联</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E8%B7%AF%E7%94%B1%E5%AF%BC%E8%88%AA%E7%AF%87"><span class="nav-text">路由导航篇</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E8%B7%AF%E7%94%B1%E8%8F%9C%E5%8D%95%E5%9B%BD%E9%99%85%E5%8C%96"><span class="nav-text">1.路由菜单国际化</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8-te-%E5%88%A4%E6%96%AD%E6%98%AF%E5%90%A6%E5%AD%98%E5%9C%A8%E8%AF%AD%E8%A8%80%E5%8C%85%E5%86%85"><span class="nav-text">* 使用 $te 判断是否存在语言包内</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B1%95%E7%A4%BA%E9%A1%B5%E9%9D%A2-1"><span class="nav-text">展示页面</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E9%9D%A2%E5%8C%85%E5%B1%91%E5%AF%BC%E8%88%AA%E5%9B%BD%E9%99%85%E5%8C%96"><span class="nav-text">2.面包屑导航国际化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%AF%BC%E8%88%AA%E5%AE%88%E5%8D%AB%E5%9B%BD%E9%99%85%E5%8C%96"><span class="nav-text">3.导航守卫国际化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-render-%E5%87%BD%E6%95%B0%E5%B1%95%E7%A4%BA%E6%96%87%E5%AD%97"><span class="nav-text">4.render 函数展示文字</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%BB%84%E4%BB%B6%E7%AF%87"><span class="nav-text">组件篇</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E9%80%89%E6%8B%A9%E6%A1%86%E3%80%81%E8%BE%93%E5%85%A5%E6%A1%86%E7%BB%84%E4%BB%B6"><span class="nav-text">1.选择框、输入框组件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E9%80%89%E6%8B%A9%E6%A1%86%E3%80%81%E8%BE%93%E5%85%A5%E6%A1%86%E8%A1%A8%E5%8D%95%E6%A0%A1%E9%AA%8C"><span class="nav-text">* 2.选择框、输入框表单校验</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%BC%B9%E5%87%BA%E6%A1%86%EF%BC%88MessageBox-%E6%88%96-Notification%EF%BC%89"><span class="nav-text">3.弹出框（MessageBox 或 Notification）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E8%87%AA%E5%88%B6%E5%BC%B9%E5%87%BA%E6%A1%86%E7%BB%84%E4%BB%B6%E5%9B%BD%E9%99%85%E5%8C%96%EF%BC%88Vue-extend%EF%BC%89"><span class="nav-text">4.自制弹出框组件国际化（Vue.extend）</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#popup-js-%E4%B8%AD%E5%A4%84%E7%90%86"><span class="nav-text">popup.js 中处理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#popup-vue-%E7%BB%84%E4%BB%B6"><span class="nav-text">popup.vue 组件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#components-%E4%B8%8B%E7%9A%84-index-js"><span class="nav-text">components 下的 index.js</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#main-js"><span class="nav-text">main.js</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%89%93%E5%8C%85%E7%AF%87"><span class="nav-text">打包篇</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE"><span class="nav-text">基础配置</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%89%93%E5%8C%85%E9%85%8D%E7%BD%AE"><span class="nav-text">打包配置</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8E%A8%E8%8D%90%E5%8F%82%E8%80%83"><span class="nav-text">推荐参考</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="LiLyn"
      src="/images/logo.jpg">
  <p class="site-author-name" itemprop="name">LiLyn</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">70</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">10</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">162</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/llwodexue" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;llwodexue" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://blog.csdn.net/qq_38689395" title="CSDN → https:&#x2F;&#x2F;blog.csdn.net&#x2F;qq_38689395" rel="noopener" target="_blank"><i class="fas fa-copyright fa-fw"></i>CSDN</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://scx69.gitee.io/blogs/" title="https:&#x2F;&#x2F;scx69.gitee.io&#x2F;blogs&#x2F;" rel="noopener" target="_blank">花开满城</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://yixin-oss.gitee.io/daytoy/" title="http:&#x2F;&#x2F;yixin-oss.gitee.io&#x2F;daytoy&#x2F;" rel="noopener" target="_blank">daytoy</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">LiLyn</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">463k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">7:01</span>
</div>

<!--
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>-->

<!--
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="powered-by">
  <i class="fa fa-user-md"></i>
  <span id="busuanzi_container_site_pv">
    本站访问量:<span id="busuanzi_value_site_pv"></span>次
  </span>
  <span class="post-meta-divider">|</span>
  <span id="busuanzi_container_site_uv">
    本站总访客量：<span id="busuanzi_value_site_uv"></span>人
  </span>
</div>-->

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  
  <script>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>













  

  

  <!-- 动态线条背景 -->
  <script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
  <!-- 页面点击小红心 -->
  <script type="text/javascript">
	!function(e,t,a){function n(){c(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"),o(),r()}function r(){for(var e=0;e<d.length;e++)d[e].alpha<=0?(t.body.removeChild(d[e].el),d.splice(e,1)):(d[e].y--,d[e].scale+=.004,d[e].alpha-=.013,d[e].el.style.cssText="left:"+d[e].x+"px;top:"+d[e].y+"px;opacity:"+d[e].alpha+";transform:scale("+d[e].scale+","+d[e].scale+") rotate(45deg);background:"+d[e].color+";z-index:99999");requestAnimationFrame(r)}function o(){var t="function"==typeof e.onclick&&e.onclick;e.onclick=function(e){t&&t(),i(e)}}function i(e){var a=t.createElement("div");a.className="heart",d.push({el:a,x:e.clientX-5,y:e.clientY-5,scale:1,alpha:1,color:s()}),t.body.appendChild(a)}function c(e){var a=t.createElement("style");a.type="text/css";try{a.appendChild(t.createTextNode(e))}catch(t){a.styleSheet.cssText=e}t.getElementsByTagName("head")[0].appendChild(a)}function s(){return"rgb("+~~(255*Math.random())+","+~~(255*Math.random())+","+~~(255*Math.random())+")"}var d=[];e.requestAnimationFrame=function(){return e.requestAnimationFrame||e.webkitRequestAnimationFrame||e.mozRequestAnimationFrame||e.oRequestAnimationFrame||e.msRequestAnimationFrame||function(e){setTimeout(e,1e3/60)}}(),n()}(window,document);
  </script>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"log":false,"model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":true},"react":{"opacity":0.7}});</script></body>
</html>